/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Booking;

import FlightSelect.FlightSelectBean;
import FlightSelect.FlightSelectPageBean;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import common.*;
import flight.FlightBean;
import javax.servlet.RequestDispatcher;

/**
 *
 * @author cipher
 */
public class BookingServlet extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    public void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        BookingBean bb = new BookingBean();
        int ID=0;//FOR MAX ID??
        String OldOrNew = request.getParameter("indicator");//indicates whether this user has existing information or not
        FlightSelectBean departFlight = (FlightSelectBean) request.getSession().getAttribute("departFlight");
        int pricetotal = ((Integer)request.getSession().getAttribute("pricetotal")).intValue();
        FlightSelectBean returnFlight = null;

        FlightBean fb = (FlightBean) request.getSession().getAttribute("fb");
        if (fb.getTripType() == 1)
            returnFlight = (FlightSelectBean) request.getSession().getAttribute("returnFlight");

        int TicketNum = fb.getNoOfTickets();
        String Email = "";
        String Exdate = "";

        if (OldOrNew.equals("0")) {// new
            String CardType = request.getParameter("RadioGroup2");
            String CardNumber = request.getParameter("cardnumber");
            String SecurityNumber = request.getParameter("securitynumber");
            String ChName = request.getParameter("chname");
            
            String Exyear = request.getParameter("exyear");
            String Exmonth = request.getParameter("exmonth");
            String Address1 = request.getParameter("Add1");
            String Address2 = request.getParameter("Add2");
            String City = request.getParameter("city");
            String State = request.getParameter("state");
            String ZipCode = request.getParameter("zipcode");
            String mID = request.getParameter("maxID");
            ID = Integer.parseInt(mID);
            Exdate = Exmonth + "-" + Exyear;

            //get from session attribute


            if (request.getSession().getAttribute("userid") == null){
                RequestDispatcher dis = request.getRequestDispatcher("/index.jsp");
                dis.forward(request,response);
            }else {
//                String Email = "w@w.com";
                Email = (String)request.getSession().getAttribute("userid");
            }
             
            bb.setCardType(CardType);
            bb.setCardNumber(CardNumber);
            bb.setAddr1(Address1);
            bb.setAddr2(Address2);
            bb.setChName(ChName);
            bb.setState(State);
            bb.setZipCode(ZipCode);
            bb.setEmail(Email);
            bb.setAddr(Address1, Address2, City, State, ZipCode);
            bb.setExdate(Exdate);

        }
        else { //old
            try {
                //old
                String mID = request.getParameter("maxID");
                ID = Integer.parseInt(mID);

                
                if (request.getSession().getAttribute("userid") == null) {
                    RequestDispatcher dis = request.getRequestDispatcher("/index.jsp");
                    dis.forward(request, response);
                } else {
//                String Email = "w@w.com";
                    Email = (String) request.getSession().getAttribute("userid");
                }

                //START A CONNECTION TO GET EMAIL
                String getExdate = "select * from USERS where USERS_EMAIL = '" + Email + "'";
                Connection con;
                con = DbConnector.getConnection();
                Statement stmt = con.createStatement();
                ResultSet rset = stmt.executeQuery(getExdate);
                rset.next();
                Exdate = rset.getString("USERS_EXPIRATION_DATE");

                rset.close();
                stmt.close();
                con.close();
                //END OF THIS BLOCK


                bb.setCardType(null);
                bb.setCardNumber(null);
                bb.setAddr1(null);
                bb.setAddr2(null);
                bb.setChName(null);
                bb.setState(null);
                bb.setZipCode(null);
                bb.setEmail(Email);
                bb.setAddr(null, null, null, null, null);
            } catch (Exception ex) {
                Logger.getLogger(BookingServlet.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        
        try
        {
            BookingPageBean bpb = new BookingPageBean();

            bpb.setBb(bb);

            if (OldOrNew.equals("0")) {
            PageBean pageCtl = bpb.Proceed(ID);
            request.setAttribute("pageCtl",pageCtl);//this contains no useful information, just ignore it
            }

            FlightSelectPageBean fspbDepart = new FlightSelectPageBean();
            FlightSelectPageBean fspbReturn = new FlightSelectPageBean();

            fspbDepart.setFsb(departFlight);
            if (fb.getTripType() == 1)
                fspbReturn.setFsb(returnFlight);

            if (fb.getSeatingClass().equals("Coach")) {
                departFlight.setseatClass(1);
                if (fb.getTripType() == 1)
                  returnFlight.setseatClass(1);
            }else {
                departFlight.setseatClass(2);
                if (fb.getTripType() == 1)
                  returnFlight.setseatClass(2);
            }

            PageBean pageCtl1 = fspbDepart.Proceed(TicketNum,1,Email, Exdate, pricetotal, ID, 1);
            PageBean pageCtl2 = null;
            if (fb.getTripType() == 1)
                pageCtl2 = fspbReturn.Proceed(TicketNum,1,Email, Exdate, 0, ID, 0);

            request.setAttribute("pageCtl1",pageCtl1);
            if (fb.getTripType() == 1)
                request.setAttribute("pageCtl2",pageCtl2);

            request.setAttribute("fb",fb);

        }
        catch (Exception e) {
             e.printStackTrace();
        }
        RequestDispatcher dis = request.getRequestDispatcher("/rightpanels/Confirmation.jsp");
        dis.forward(request,response);

    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
